<?php

namespace App\Http\Controllers;

use App\Models\Group;
use App\Models\Order;
use App\Models\OrderRoom;
use App\Models\Room;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Inertia\Inertia;

class OrderController extends Controller
{
    public function index(Request $request)
    {
        $groups = Group::query()->select('*')->with([
            'rooms' => function ($query) {
                return $query->select('*');
            }
        ])->get();

        $orders = Order::query()->select('*')->with([
            'rooms' => function ($query) {
                return $query->select('*');
            },
            'customer' => function ($query){
                return $query->select('*');
            },
            'user' => function ($query){
                return $query->select('*');
            }
        ])->get();

        return Inertia::render('Booking/Order', [
            'orders' => $orders,
            'groups' => $groups
        ]);
    }

    public function calendar(Request $request)
    {
        $month = $request->get('month');
        $room = $request->get('room');
        $group = $request->get('group');

        $orderQuery = Order::query()->select('*');

        if($group){
            $rooms = Group::query()->find($group)->rooms()->get();
            $roomIds = array();
            foreach ($rooms as $key=>$room){
                $roomIds[$key] = $room->id;
            }
            $orderRooms = OrderRoom::query()->whereIn('room_id',$roomIds)->get();
            $orderIds = array();
            foreach ($orderRooms as $key=>$or){
                $orderIds[$key] = $or->order_id;
            }
            $orderQuery = Order::query()->whereIn('id',$orderIds);
        } elseif ($room){
            $orderRooms = OrderRoom::query()->where('room_id',$room)->get();
            $orderIds = array();
            foreach ($orderRooms as $key=>$or){
                $orderIds[$key] = $or->order_id;
            }
            $orderQuery = Order::query()->whereIn('id',$orderIds);
        }
        $month = !is_null($month) ? $month : Carbon::now()->format('Y-m');

        if($month){
            $start=date('Y-m-01',strtotime($month));//获取指定月份的第一天
            $end=date('Y-m-t',strtotime($month)); //获取指定月份的最后一天
            $orderQuery->whereBetween('order_date',[$start,$end]);
        }

        $orders = $orderQuery->with([
            'rooms' => function ($query) {
                return $query->select('*');
            },
            'customer' => function ($query){
                return $query->select('*');
            }
        ])->get();

        $groups = Group::query()->select('*')->with([
            'rooms' => function ($query) {
                return $query->select('*');
            }
        ])->get();

        $results = array();
        foreach ($orders as $order){
            $flag = true;
            foreach ($results as $key=>$result){
                if ($order->order_date == $result['order_date']){
                    $result['orders'][] = $order;
                    $flag = false;
                }
                $results[$key] = $result;
            }
            if ($flag){
                $results[] = [
                    'order_date'=>$order->order_date,
                    'orders'=>[$order]
                ];
            }
        }

        return Inertia::render('Booking/Calendar', [
            'orders' => $results,
            'groups' => $groups
        ]);
    }

    public function update(Request $request){
        $user = $request->user();
        $origin = $request->get('origin');
        $route = $origin ? $origin : 'orders';
        $data = $request->all();
        Order::dataSave($data, $user);

        return Redirect::route($route);
    }

    public function delete(Request $request){
        $id = $request->get('id');
        Order::query()->find($id)->delete();
        OrderRoom::query()->where('order_id',$id)->delete();

        return Redirect::route('orders');
    }

    public function status(Request $request){
        $data = $request->all();
        $order = Order::query()->find($data['id']);
        $order->status = $data['status'];
        $order->save();

        return Redirect::route('orders');

    }

}
